package com.enniswu.weitter.widget.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.enniswu.weitter.R;
import com.enniswu.weitter.data.local.DBSupport;
import com.enniswu.weitter.entity.Post;
import com.enniswu.weitter.entity.User;
import com.enniswu.weitter.moment.posting.PostingActivity;
import com.enniswu.weitter.user.info.UserInfoActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {

    private List<Post> posts;
    private OnItemClickListener clickListener = new SimpleOnItemClickListener();

    public PostAdapter(List<Post> posts) {
        this.posts = posts;
    }

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_post, parent, false);
        final PostViewHolder holder = new PostViewHolder(view);

        View.OnClickListener l = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener == null)
                    return;
                int position = holder.getAdapterPosition();
                switch (v.getId()) {
                    case R.id.iv_portrait:
                        clickListener.onPortraitClick(v, position);
                        break;
                    default:
                        clickListener.onItemClick(v, position);
                        break;
                }
            }
        };
        view.setOnClickListener(l);
        view.findViewById(R.id.iv_portrait).setOnClickListener(l);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        Post post = posts.get(position);
        User user = DBSupport.getInstance().findUser(post.getUserId(), false);
        if (user != null) {
            Glide.with(holder.ivPortrait).load(user.getPortrait())
                    .apply(new RequestOptions().circleCrop()).into(holder.ivPortrait);
            holder.tvName.setText(user.getName());
        }
        holder.tvTime.setText(post.getCreateTime());
        holder.tvContext.setText(post.getContext());
        if (holder.rvAlbum.getAdapter() == null) {
            holder.rvAlbum.setAdapter(new AlbumAdapter(post.getAlbum()));
        } else {
            ((AlbumAdapter) holder.rvAlbum.getAdapter()).setAlbum(post.getAlbum());
        }
        holder.rvAlbum.getAdapter().notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }

    static class PostViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_portrait)
        ImageView ivPortrait;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_variety)
        TextView tvTime;
        @BindView(R.id.tv_context)
        TextView tvContext;
        @BindView(R.id.rv_album)
        RecyclerView rvAlbum;

        public PostViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            rvAlbum.setNestedScrollingEnabled(false);
            rvAlbum.setLayoutManager(new GridLayoutManager(rvAlbum.getContext(), 3));
        }
    }

    public interface OnItemClickListener extends com.enniswu.weitter.widget.adapter.OnItemClickListener {
        void onPortraitClick(View v, int position);
    }

    public class SimpleOnItemClickListener implements OnItemClickListener {
        @Override
        public void onPortraitClick(View v, int position) {
            Post post = posts.get(position);
            User user = DBSupport.getInstance().findUser(post.getUserId(), false);
            if (user != null) {
                Intent intent = new Intent(v.getContext(), UserInfoActivity.class);
                intent.putExtra("user", user);
                v.getContext().startActivity(intent);
            }
        }

        @Override
        public void onItemClick(View v, int position) {
            Post post = posts.get(position);
            Intent intent = new Intent(v.getContext(), PostingActivity.class);
            intent.putExtra("post", post);
            v.getContext().startActivity(intent);
        }
    }
}
